var fs = require('fs')
//框架工具模块
var vmtools = require(__dirname + "\\tools\\tools.node.js");
var htmlelements = require(__dirname + "\\browser\\HTMLElements\\htmlelements.node.js");
var audio = require(__dirname + "\\browser\\Audio\\audio.node.js");
var other = require(__dirname + "\\browser\\other\\other_getcode.node.js");
var performance = require(__dirname + "\\browser\\Performances\\per_getcode.node.js");

function GetCode(config, func, dom_arr) {
    var code = "";
    //引入框架工具代码
    code += vmtools.GetCode() + "\r\n";

    //引入用户框架配置
    for (var i in config) {
        code += 'catvm.memory.config.' + i + '=' + config[i] + ';\r\n';
    }
    code += func + ';\r\n';
    // code += "catvm.memory.config.proxy = true; \r\n";   //打开代理
    // 引入浏览器相关
    // window.__proto__  ==>  WindowProperties.__proto__  ==>  EventTarget   所以得先对EventTarget加载
    code += fs.readFileSync(__dirname + `\\browser\\EventTarget.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\Nodes.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\WindowProperties.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\DOMParser.js`) + "\r\n"
    // bom
    code += fs.readFileSync(__dirname + `\\browser\\Window.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\Location.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\History.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\XMLHttpRequestEventTarget_.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\XMLHttpRequest_.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\Navigator.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\Screen.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\Storage.js`) + "\r\n"
    //加载navigation 插件环境
    code += fs.readFileSync(__dirname + `\\browser\\MimeType.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\Plugin.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\PluginArray.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\MimeTypeArray.js`) + "\r\n"
    //加载performance 环境
    code += performance.GetCode() + "\r\n";
    //加载音频环境
    code += audio.GetCode() + "\r\n";
    //加载杂项环境
    code += other.GetCode() + "\r\n";
    //dom环境需要在最后加载
    code += htmlelements.GetCode() + "\r\n";
    code += fs.readFileSync(__dirname + `\\browser\\Document.js`) + "\r\n"
    code += fs.readFileSync(__dirname + `\\browser\\Image_.js`) + "\r\n"
    //引入用户自定义环境
    code += dom_arr + ';\r\n';

    // 加入hook代码
    code += fs.readFileSync(__dirname + `\\tools\\hook.js`) + "\r\n"
    // 清楚框架日志
    code += "console.clear()\n;debugger;\r\n";
    return code;
}

module.exports = {
    GetCode
}